package com.kocel.block.service.Impl;
import com.kocel.block.model.TbBlockInventory;
import com.kocel.block.model.TbBlockRecord;
import com.kocel.block.model.TbUser;
import com.kocel.block.model.smelt_pouring_history;
import com.kocel.block.repository.*;
import com.kocel.block.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BlockServiceImpl implements BlockService {
    @Autowired
    //private JdbcRepository jdbcRepository;
    private ViewRepository viewRepository;

    @Autowired
    private TbUserRepository tbUserRepository;

    @Autowired
    private TbBlockInventoryRepository tbBlockInventoryRepository;

    @Autowired
    private TbBlockRecordRepository tbBlockRecordRepository;

    @Override
    public TbBlockInventoryRepository getTbBlockInventoryRepository() {
        return tbBlockInventoryRepository;
    }

    @Override
    public TbUserRepository getTbUserRepository() {
        return tbUserRepository;
    }

    @Override
    public TbBlockRecordRepository getTbBlockRecordRepository() {
        return tbBlockRecordRepository;
    }

    @Override
    public List<smelt_pouring_history> findAllByView(String meltingStove,String meltingWrap,String texture) {
        List<smelt_pouring_history> list = null;
        if(meltingStove.equals("") && meltingWrap.equals("") && texture.equals("")){
            list = viewRepository.getSmelt_pouring_historyTop100();
        }else{
            list = viewRepository.getSmelt_pouring_historyList(meltingStove,meltingWrap,texture);
        }

        return list;
    }

    @Override
    public List<smelt_pouring_history> findAllByMeltingWrap(String meltingWrap) {
        return viewRepository.getSmelt_pouring_historyByMeltingWrap(meltingWrap);
    }

    @Override
    public List<smelt_pouring_history> findAllByMeltingStove(String meltingStove) {
        return viewRepository.getSmelt_pouring_historyByMeltingStove(meltingStove);
    }

    @Override
    public TbUser getUser(String username, String password) {
        return tbUserRepository.findTbUserByUserNameAndPassword(username, password);
    }

    @Override
    public List<TbBlockInventory> getInventoryList(String productName, String blockNumber, String meltingStove) {
        List<TbBlockInventory> retList = new ArrayList<>();
        if(!productName.equals("")){
            List<TbBlockInventory> list = tbBlockInventoryRepository.findByProductContaining(productName);
            retList.addAll(list);
        }

        if(!blockNumber.equals("")){
            List<TbBlockInventory> list = tbBlockInventoryRepository.findByBlockNumberContaining(blockNumber);
            retList.addAll(list);
        }

        if(!meltingStove.equals("")){
            List<TbBlockInventory> list = tbBlockInventoryRepository.findByMeltingStoveContaining(meltingStove);
            retList.addAll(list);
        }

        if(meltingStove.equals("") && blockNumber.equals("") && productName.equals("")){
            List<TbBlockInventory> list = tbBlockInventoryRepository.findAll();
            retList.addAll(list);
        }
        return retList;
    }

    @Override
    public boolean isExistedBlockNo(String blockNo) {
        return tbBlockInventoryRepository.existsByBlockNumber(blockNo);
    }

    @Override
    public List<TbBlockInventory> getTbBlockInventoryListForOut(String blockNumber) {

        return blockNumber.equals("") ? tbBlockInventoryRepository.findForOut1(blockNumber): tbBlockInventoryRepository.findForOut2(blockNumber);
    }

    @Override
    public List<TbBlockInventory> getTbBlockInventoryListForDestroy(String blockNumber) {
        return blockNumber.equals("") ? tbBlockInventoryRepository.findForDestroy1(blockNumber): tbBlockInventoryRepository.findForDestroy2(blockNumber);
    }

    @Override
    public List<TbBlockRecord> getTbBlockRecordList(String blockNumber) {
        return tbBlockRecordRepository.findForList(blockNumber);
    }

    @Override
    public List<TbBlockRecord> getTbBlockRecordOutList(String blockNumber) {
        return tbBlockRecordRepository.findForOut(blockNumber);
    }

    @Override
    public List<TbBlockRecord> getTbBlockRecordOutReturnList(String blockNumber) {
        return tbBlockRecordRepository.findForOutReturn(blockNumber);
    }

    @Override
    public List<TbBlockRecord> getTbBlockRecordCheckList(String blockNumber) {
        return tbBlockRecordRepository.findForCheck(blockNumber);
    }

    @Override
    public List<TbBlockRecord> getTbBlockRecordMatchingList(String blockNumber) {
        return tbBlockRecordRepository.findForMatching(blockNumber);
    }


}
